import json
import pywbem

# SMI-S 服务器信息
SMIS_HOST = "https://10.2.29.111"
SMIS_USER = "3paradm"
SMIS_PASS = "OPS@2528sjzx"
SMIS_NAMESPACE = "root/tpd"  # SMI-S 默认命名空间

# 连接 SMI-S 服务器
conn = pywbem.WBEMConnection(SMIS_HOST, (SMIS_USER, SMIS_PASS), no_verification=True)


def get_storage_pools():
    """获取存储池信息"""
    try:
        instances = conn.EnumerateInstances("CIM_StoragePool", namespace=SMIS_NAMESPACE)
        for inst in instances:
            name = inst['ElementName']
            capacity = int(inst['TotalManagedSpace']) / (1024**3)  # 转换为GB
            free_space = int(inst['RemainingManagedSpace']) / (1024**3)  # 转换为GB
            print(f"存储池: {name}, 总容量: {capacity:.2f} GB, 剩余空间: {free_space:.2f} GB")
    except Exception as e:
        print(f"获取存储池信息失败: {e}")

def get_logical_volumes():
    """查询 HPE 3PAR 逻辑卷信息"""
    try:
        instances = conn.EnumerateInstances("CIM_StorageVolume", namespace=SMIS_NAMESPACE)
        for inst in instances:
            name = inst.get('ElementName', 'Unknown')
            block_size = inst.get('BlockSize', 1)
            num_blocks = inst.get('NumberOfBlocks', 0)
            size_gb = (block_size * num_blocks) / (1024 ** 3)  # 转换为 GB
            status = inst.get('OperationalStatus', 'Unknown')
            print(f"逻辑卷: {name}, 容量: {size_gb:.2f} GB, 状态: {status}")
    except Exception as e:
        print(f"获取逻辑卷信息失败: {e}")

def get_cpu_info():
    """获取 CPU 信息"""
    try:
        instances = conn.EnumerateInstances("CIM_Processor", namespace=SMIS_NAMESPACE)
        for inst in instances:
            name = inst['DeviceID']
            status = inst['OperationalStatus'][0]
            print(json.dumps({"cpu_name": name, "cpu_status": status}))
    except Exception as e:
        print(f"获取 CPU 信息失败: {e}")

def get_memory_info():
    """获取内存信息"""
    try:
        instances = conn.EnumerateInstances("CIM_PhysicalMemory", namespace=SMIS_NAMESPACE)
        for inst in instances:
            size = int(inst['Capacity']) / (1024**3)  # 转换为GB
            status = inst['OperationalStatus']
            print(f"内存: {size:.2f} GB, 状态: {status}")
    except Exception as e:
        print(f"获取内存信息失败: {e}")

def get_disk_info():
    """获取 HPE 3PAR 硬盘信息"""
    try:
        instances = conn.EnumerateInstances("CIM_StorageExtent", namespace=SMIS_NAMESPACE)
        for inst in instances:
            print(inst)
            name = inst.get('DeviceID', 'Unknown')
            size = inst.get('BlockSize', 1) * inst.get('NumberOfBlocks', 0) / (1024**3)  # 转换为 GB
            status = inst.get('OperationalStatus', 'Unknown')
            print(f"硬盘: {name}, 容量: {size:.2f} GB, 状态: {status}")
    except Exception as e:
        print(f"获取硬盘信息失败: {e}")


def get_fan_info():
    """获取风扇状态"""
    try:
        instances = conn.EnumerateInstances("CIM_Fan", namespace=SMIS_NAMESPACE)
        for inst in instances:
            name = inst['DeviceID']
            status = inst['OperationalStatus']
            speed = inst.get('VariableSpeed', 'N/A')
            print(f"风扇: {name}, 状态: {status}, 转速: {speed}")
    except Exception as e:
        print(f"获取风扇信息失败: {e}")

def get_power_info():
    """获取电源状态"""
    try:
        instances = conn.EnumerateInstances("CIM_PowerSupply", namespace=SMIS_NAMESPACE)
        for inst in instances:
            name = inst['ElementName']
            status = inst['OperationalStatus']
            print(f"电源: {name}, 状态: {status}")
    except Exception as e:
        print(f"获取电源信息失败: {e}")

if __name__ == "__main__":
    #get_storage_pools()
    #get_logical_volumes()
    get_cpu_info()
    #get_memory_info()
    #get_disk_info()
    #get_fan_info()
    #get_power_info()